package com.sec.android.app.myfiles.operation;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.AppConstants.StoragePath;
import com.sec.android.app.myfiles.info.MimeType;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.util.GolfUtils;
import com.sec.android.app.myfiles.util.SemFwWrapper;

import java.io.File;

/**
 * This class requires 'Qdio.jar' to share SoundShot file. 'Qdio.jar' is offered
 * by 'Quramsoft'. If you need to contact this solution company, contact a
 * developing visual media solution part of Camera R&D Group at first.
 */

public class ConvertAsyncTask extends AsyncTask<ConvertAsyncTask.ConvertInfo, Void, String> {
    private static final String TAG = ConvertAsyncTask.class.getSimpleName();
    private static final String CONVERT_FILE_TEMP_PATH = StoragePath.INTERNAL_ROOT + "/RW_LIB/";
    private static final String EXTENSION_MP4 = ".mp4";
    private OnConvertTaskListener mTaskListener;

    public interface OnConvertTaskListener {
        void onTaskComplete(String result);
    }

    public static class ConvertInfo {
        public String path;
        public String mimeType;
        public int convertType;
    }

    public ConvertAsyncTask() {
    }

    public void setOnTaskListener(OnConvertTaskListener l) {
        mTaskListener = l;
    }

    @Override
    protected String doInBackground(ConvertAsyncTask.ConvertInfo... params) {
        String ret = null;
        ConvertInfo info = params[0];

        switch (info.convertType) {

            case AppConstants.ConvertType.AS_VIDEO_FILES:
                ret = convertFile(info.path);
                break;

            case AppConstants.ConvertType.AS_IMAGE_FILES:
                if ((MimeType.IMAGE_GOLF).equalsIgnoreCase(info.mimeType)) {
                    String filePath = GolfUtils.getJpgTempFilePath(info.path, false);

                    if (TextUtils.isEmpty(filePath)) {
                        break;
                    }
                    ret = "/" + filePath;
                    break;
                }
                ret = info.path;
                break;

            default:
                break;
        }

        return ret;
    }

    @Override
    protected void onPostExecute(String result) {
        File folder = SemFwWrapper.file(CONVERT_FILE_TEMP_PATH);
        if (folder.exists()) {
            deleteGolfFile(folder);
        }

        if (mTaskListener != null) {
            mTaskListener.onTaskComplete(result);
        }
    }

    private String convertFile(String path) {
        String target = path.substring(0, path.lastIndexOf('.')) + EXTENSION_MP4;

        if (SemFwWrapper.file(target).exists()) {
            return target;
        }

        File folder = SemFwWrapper.file(CONVERT_FILE_TEMP_PATH);
        if (!folder.exists()) {
            if (folder.mkdir() == false) {
                Log.d(this, "convertFile : mkdir failed");
            }
        }

        return target;
    }

    private void deleteGolfFile(File file) {
        if (!file.delete()) {
            File[] subFiles = file.listFiles();
            if (subFiles != null && subFiles.length != 0) {
                for (File f : subFiles) {
                    deleteGolfFile(f);
                }

                if (file.delete() == false) {
                    Log.d(this, "deleteGolfFile : delete failed");
                }
            }
        }
    }
}
